package com.ruanyuan.springboot.service.impl;

import com.ruanyuan.springboot.dao.CarDao;
import com.ruanyuan.springboot.dao.EmployeeDao;
import com.ruanyuan.springboot.pojo.Car;
import com.ruanyuan.springboot.pojo.Employee;
import com.ruanyuan.springboot.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 用户表业务层接口实现类
 */
@Service
public class EmployeeServiceImpl implements EmployeeService {
    @Autowired
    private EmployeeDao EmployeeDao;
    @Autowired
    private CarDao CarDao;
    /**
     * 通过用户名称查询
     * @param username
     * @return
     */
    @Override
    public Employee selectEmployeeUsername(String username) {
        return this.EmployeeDao.selectEmployeeUsername(username);
    }

    /**
     * 查询用户信息
     * @param page
     * @param limit
     * @return
     */
    @Override
    public List<Employee> selectEmployee(int page, int limit) {
        return this.EmployeeDao.selectEmployee(page, limit);
    }

    /**
     * 查询用户个数
     * @return
     */
    @Override
    public int selectEmployeeCount() {
        return this.EmployeeDao.selectEmployeeCount();
    }

    /**
     * 添加用户信息
     * @param employee
     * @return
     */
    @Override
    public int insertEmployee(Employee employee) {
        return this.EmployeeDao.insertEmployee(employee);
    }

    /**
     * 修改用户信息
     * @param employee
     * @return
     */
    @Override
    public int updateEmployee(Employee employee) {
        int count =  this.EmployeeDao.updateEmployee(employee);
        if(count > 0){
            return 2;
        }else{
            return 0;
        }
    }

    /**
     * 单个删除用户信息
     * @param id
     * @return
     */
    @Override
    public int deleteEmployeeById(String id) {
        List<Car> carList = this.CarDao.selectCaremployeeid(id);
        if(carList.size()>0) {
            this.CarDao.deleteCarByEmployeeId(id);
        }
        return this.EmployeeDao.deleteEmployeeById(id);
    }

    /**
     * 批量删除用户信息
     * @param empids
     * @return
     */
    @Override
    public int deleteEmployeeIds(String[] empids) {
        for (String id : empids) {
            List<Car> carList = this.CarDao.selectCaremployeeid(id);
            if(carList.size()>0){
                this.CarDao.deleteCarByEmployeeId(id);
            }
        }
        // 创建List集合储存id
        List<String> ids1 = new ArrayList<String>();
        // 遍历数组，储存在集合内
        for (String id : empids) {
            ids1.add(id);
        }
        return this.EmployeeDao.deleteEmployeeIds(ids1);
    }
}
